// client project main.go
package main

import (
	"crypto/rand"
	"fmt"
	"io/ioutil"
	"math/big"
	"net/http"
	"net/url"
	"strconv"
)

var ASCII string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
var digit string = "0123456789"

func genName() string {
	tmp, _ := rand.Int(rand.Reader, big.NewInt(6))
	val := int(tmp.Int64())

	var name string = ""
	for i := 0; i < 6+val; i++ {
		tmp, _ := rand.Int(rand.Reader, big.NewInt(int64(len(ASCII))))
		index := int(tmp.Int64())
		name = name + string(ASCII[index])
	}

	return name
}

func genMobile() string {
	var mobile string = "1"
	for i := 0; i < 10; i++ {
		tmp, _ := rand.Int(rand.Reader, big.NewInt(int64(len(digit))))
		number := int(tmp.Int64())
		mobile = mobile + strconv.Itoa(number)
	}

	return mobile
}
func doRequest(ch chan int, index int) {
	for {
		passwd := genName()
		data := url.Values{}
		data.Add("username", genName())
		data.Add("passwd", passwd)
		data.Add("confirm", passwd)
		data.Add("mobile", genMobile())
		data.Add("channel", "1000")
		data.Add("platform", "0")
		resp, err := http.PostForm("http://www.simpdo.com:8000/regist", data)
		if err != nil {
			fmt.Println("post form error, ", err.Error())
			ch <- index
		}

		defer resp.Body.Close()
		body, _ := ioutil.ReadAll(resp.Body)
		fmt.Println(string(body))
	}

	ch <- index
}

func main() {
	chs := make([]chan int, 20)

	for i := 0; i < 20; i++ {
		chs[i] = make(chan int)
		go doRequest(chs[i], i)
	}

	for _, ch := range chs {
		<-ch
	}
}
